<template>
    <div class="websocket">
        <background></background>
        <operation></operation>
    </div>
</template>

<script type="text/ecmascript-6">
  import Background from "../../components/common/Background";
  import Operation from "../../components/common/Operation";

  import SockJS from "sockjs-client";
  import Stomp from "stompjs";

  /**
   * @author 白雨浓
   * @date 2018/10/30 15:35
   *
   * Websocket
   **/
  export default {
    name: 'Websocket',
    components: {Operation, Background},
    data() {
      return {
        websocket: null
      }
    },
    created() {
      this.init()
    },
    destroyed: function () {
      this.close();
    },
    mounted() {
      this.$nextTick(() => {

      })
    },
    methods: {
      init() {
        const wsURL = 'http://localhost:8080/ws/';//ws地址
        this.websock = new SockJS(wsURL);
        this.websocket.onopen = this.open();
        this.websocket.onerror = this.error();
        this.websock.onmessage = this.message();
        this.websock.onclose = this.close();
      },
      open() {
        console.log("WebSocket连接成功");
      },
      error() {
        console.log("WebSocket连接发生错误");
      },
      message(msg) {
        console.log(msg);
      },
      send(msg) {//数据发送
        this.websock.send(msg);
      },
      close(e) {
        console.log("connection closed (" + e.code + ")");
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
    .websocket {
    }
</style>
